﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace HtControl
{
    public partial class MyButton : Button
    {
        public MyButton()
        {
            InitializeComponent();
        }

        public enum State
        {
            normal,
            focused,
            mousedown
        }

        private State buttonState = State.normal;
        [Browsable(false)]
        public State ButtonState
        {
            get
            {
                return buttonState;
            }
            set
            {
                buttonState = value;
            }
        }

        protected Image normalImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton_s.Res.blue.png"));
        [Category("自定义"), Description("正常状态的背景图片")]
        [Browsable(true)]
        public Image NormalImage
        {
            get
            {
                return normalImage;
            }
            set
            {
                normalImage = value;
                BackgroundImage = value;
            }
        }

        protected Image fousedImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton_s.Res.red.png"));
        [Category("自定义"), Description("高亮状态的背景图片")]
        [Browsable(true)]
        public Image FousedImage
        {
            get
            {
                return fousedImage;
            }
            set
            {
                fousedImage = value;
            }
        }

        protected Image mouseDownImage = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton_s.Res.green.png"));
        [Category("自定义"), Description("获取焦点状态的背景图片")]
        [Browsable(true)]
        public Image MouseDownImage
        {
            get
            {
                return mouseDownImage;
            }
            set
            {
                mouseDownImage = value;
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            buttonState = State.focused;
            base.OnMouseEnter(e);
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            buttonState = State.normal;
            base.OnMouseLeave(e);
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            buttonState = State.mousedown;
            base.OnMouseDown(mevent);
            Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            if (ClientRectangle.Contains(mevent.X, mevent.Y))
            {
                buttonState = State.focused;
            }
            else
            {
                buttonState = State.normal;
            }
            base.OnMouseUp(mevent);
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            base.OnPaintBackground(pevent);

            Graphics g = pevent.Graphics;

            switch (buttonState)
            {
                case State.focused:
                    g.DrawImage(fousedImage, new Rectangle(0, 0, Width, Height));
                    break;
                case State.mousedown:
                    g.DrawImage(mouseDownImage, new Rectangle(0, 0, Width, Height));
                    break;
                default:
                    g.DrawImage(normalImage, new Rectangle(0, 0, Width, Height));
                    break;
            }
            Console.WriteLine(buttonState + " in MyButton");
        }
    }
}
